package com.rm.system.model;

import com.jfinal.core.Controller;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Model;
import com.rm.common.shiro.ShiroExt;
import com.rm.system.Constants;
import com.rm.system.util.IpUtil;

public class Log extends Model<Log> {

	private static final long serialVersionUID = 5431490874822813769L;
	private Logger log = Logger.getLogger(getClass());
	public static final Log dao = new Log();

	public static final int EVENT_VISIT = 1;
	public static final int EVENT_LOGIN = 2;
	public static final int EVENT_ADD = 3;
	public static final int EVENT_UPDATE = 4;
	public static final int EVENT_DELETE = 5;
	public static final int EVENT_GRANT = 6;
	public static final String LEVEL_INFO = "I";
	public static final String LEVEL_WARN = "W";
	public static final String LEVEL_ERROR = "E";

	public void insert(Controller con, int operation) {
		insert(con, operation, LEVEL_INFO);
	}
	public void insert(Controller con, int operation,String level) {
		// TODO Auto-generated method stub
		String ip = IpUtil.getIp(con.getRequest());
		String from = con.getRequest().getHeader("Referer");
		User user = ShiroExt.getSessionAttr(Constants.SESSION_USER);
//		Log event = new Log().set("ip", ip).set("from", from);
//		if (user != null)
//			event.set("uid", user.getId());

		// 持久化
		// event.set("operation", operation).saveAndDate();
		
		log.info("user:"+user.getName()+",ip:"+ip+",from:"+from+",op:"+operation+",level:"+level);
	}

}
